package in.unlocker.apps.unlock.service;



import in.unlocker.apps.unlock.common.encrypt.EncryptionService;
import in.unlocker.apps.unlock.dao.MasterDataRepository;
import in.unlocker.apps.unlock.mail.EmailSender;
import in.unlocker.apps.unlock.mail.EmailTemplates;
import in.unlocker.apps.unlock.mail.Template;
import in.unlocker.apps.unlock.model.Profile;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public abstract class AbstractService {
	@Autowired
	protected MasterDataRepository masterDataRepository;

	@Autowired
	protected EmailSender emailService;
	
	@Autowired
	protected EncryptionService encryptionService;

	@Value("${hostName}")
	protected String hostName;
	@Value("${adminUser}")
	protected String adminUser;
	@Value("${productName}")
	protected String productName;

	// check if we still need this setter method?
	public void setEmailService(EmailSender emailService) {
		this.emailService = emailService;
	}

	public Profile getLoggedInUser() {
		SecurityContext context = SecurityContextHolder.getContext();
		Authentication authentication = context.getAuthentication();
		ExtendedUser user = (ExtendedUser) authentication.getPrincipal();

		return (Profile) masterDataRepository.getById(Profile.class,
				user.getId());
	}

	public Integer getLoggedInUserId() {
		SecurityContext context = SecurityContextHolder.getContext();
		Authentication authentication = context.getAuthentication();
		ExtendedUser user = (ExtendedUser) authentication.getPrincipal();

		return user.getId();
	}

	public void updateBasicEmailSettings(Map<String, String> settings) {

		settings.put(EmailTemplates.adminUser, adminUser);
		settings.put(EmailTemplates.productName, productName);
		settings.put(EmailTemplates.hostName, hostName);

	}

	public void sendEmail(String emailId, Template template,
			Map<String, String> parameters) {
		String subject = null;
		String emailText = null;
		String firstName = parameters.get(EmailTemplates.firstName);

		if (firstName != null)
			firstName = Character.toUpperCase(firstName.charAt(0))
					+ firstName.substring(1);

		updateBasicEmailSettings(parameters);
		switch (template) {

		case Registration:
			emailText = EmailTemplates.getEmailMessage(Template.Registration,
					parameters);
			subject = "Thank you for registration with ResourceMart";
			break;
		case ForgotPassword:
			emailText = EmailTemplates.getEmailMessage(Template.ForgotPassword,
					parameters);
			subject = "Dear " + firstName + " Your password details inside";
			break;
	
		case campaignForApproval:
			emailText = EmailTemplates.getEmailMessage(
					Template.campaignForApproval, parameters);
			subject = "Dear " + firstName + ", New Case for Approval ";
			break;

		case campaignRegistered:
			emailText = EmailTemplates.getEmailMessage(Template.campaignRegistered,
					parameters);
			subject = "Dear " + firstName
					+ ", A new Job is posted at ResourceMart";
			break;

		default:

		}

		emailService.sendMailWithSSL(subject, emailText, emailId);
	}
}
